package com.junyidao.game.interceptor;

import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.pojo.SysUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 * Created by zzh on 2016/7/8.
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //不需要授权、认证的目录
        String[] arr = new String[]{"/login", "/register"};
        String uri = req.getRequestURI() + "?" + req.getQueryString();
        for (String str : arr) {
            if (uri.indexOf(str) != -1) {
                return true;
            }
        }
        SysUser user = LoginInfo.getUser();
        if (user == null) {
            httpServletResponse.sendRedirect("/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
